home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byt87ibm.arc / SAGAN.ARC / MOUSESYS.ASM < prev   
Assembly Source File  |  1987-06-17  |  7KB  |  255 lines

  1.     ASSUME CS:CSEG, DS:CSEG, ES:NOTHING, SS:NOTHING 
  2. SERIAL    EQU    14H 
  3. MSDOS    EQU    21H 
  4. ; This is the main entry point 
  5. ; all driver routines take the function call number in BX 
  6. ;     function 0 = initialize mouse 
  7. ;     function 1 = return button status 
  8. ;     function 2 = return relative motion 
  9. ;     function 3 = de-initialize mouse 
  10. ;     function 4 = return current serial port 
  11. ; Normally this would be a far procedure but to avoid getting into 
  12. ; all the intricasies of loading and calling drivers I've converted 
  13. ; ENTRY to a near procedure and combined it with the sample program. 
  14. ENTRY    PROC    NEAR  
  15.     CLD              ; go in the forward direction 
  16.     PUSH    DS        ; save callers segment 
  17.     PUSH    CS        ; make this segment addressable 
  18.     POP    DS 
  19.     SHL    BX,1        ; point to routine 
  20.     CALL    ROUTINES[BX]    ; and call it through table 
  21.     POP    DS        ; restore users segment 
  22.     RET            ; return far to caller 
  23. ENTRY    ENDP 
  24.     DB    'Mouse systems',00    ; name 
  25. ROUTINES LABEL WORD 
  26.     DW    ISERIAL        ; function 0 = initialize mouse 
  27.     DW    BUTTONSTAT    ; function 1 = return button status 
  28.     DW    MOTIONCOUNT    ; function 2 = return relative motion 
  29.     DW    DSERIAL        ; function 3 = de-initialize mouse 
  30.     DW    GSERIAL        ; function 4 = return current serial port 
  31.     DW    RETADR        ; function 5 = reserved 
  32.     DW    RETADR        ; function 6 = reserved 
  33.     DW    RETADR        ; function 7 = reserved 
  34. COMNUM    DW    00        ; com# 
  35. NEWX    DW    00        ; New x coordinate 
  36. NEWY    DW    00        ; New y coordinate 
  37. XACCUM    DW    0        ; Old x coordinate     
  38. YACCUM    DW    0        ; Old y coordinate 
  39. BSTAT    DB    07H        ; button status byte 
  40. CPORT    DW    03F8H        ; communications port address 
  41. PCOUNT    DB    0        ; packet counter 
  42. IMSK    DB    0EFH        ; interrupt mask 
  43. ; This is the heart of the code. 
  44. ; The serial interrupt handler. This code catches serial bytes and maintains 
  45. ; a running total of delta x and delta y values. 
  46. ;  
  47. ISR: 
  48.     STI            ; Ints back on 
  49.     PUSH    AX        ; Save all registers used 
  50.     PUSH    BX 
  51.     PUSH    DX 
  52.     PUSH    DS 
  53.     PUSH    CS 
  54.     POP    DS        ; make Code SEGment addressable 
  55.     MOV    DX,CPORT    ; get port address     
  56.     ADD    DX,5        ; Status  
  57.     IN    AL,DX        ; Read status 
  58.     MOV    AH,AL        ; Save in Ah 
  59.     SUB    DX,5        ; back to data port address 
  60.     IN    AL,DX        ; get byte from port 
  61.     AND    AH,01EH        ; mask error bits of status 
  62.     JNZ    ISR3        ; jmp if error 
  63. ; Jump if an error has occured on the serial line! most likely an overrun
  64. ; error caused by interrupts cleared for long periods of time. 
  65. ; This will be handled simply by clearing the packet counter.  
  66. ISR2: 
  67.     CMP    PCOUNT,0    ; Is this the first byte of packet ? 
  68.     JNE    ISR25        ; no so accumulate. 
  69.     MOV    AH,AL        ; it is the first byte so check certain 
  70.     AND    AH,0F8H        ; bits to se if we're in sync with the 
  71.     CMP    AH,080H        ; data stream. If we're not then 
  72.     JNZ    ISR4        ; we'll just return 
  73.     MOV    BSTAT,AL    ; we are in sync so stuff button status byte  
  74. ISR25: 
  75.     MOV    BL,PCOUNT    ; get packet counter 
  76.           INC    PCOUNT        ; increment for next serial interrupt 
  77.     OR    BL,BL        ; if it's zero we're done 
  78.     JZ    ISR4 
  79.     CBW            ; Convert delta byte to delta word 
  80.     TEST    BL,1        ; Check if odd or even. odd = x values 
  81.     JZ    ADDY        ; even = y values 
  82.     ADD    XACCUM,AX    ; add to running x accumulator 
  83.     JMP    SHORT ISR29 
  84. ADDY: 
  85.     ADD    YACCUM,AX    ; or add to running y accumulator 
  86. ISR29: 
  87.           CMP    BL,4        ; end of packet  
  88.     JB    ISR4        ; no 
  89. ISR3:
  90.     MOV    PCOUNT,0    ; yes, so reset packet counter 
  91. ISR4: 
  92.     CLI 
  93.     MOV    AL,020H        ; must issue EOI 
  94.     OUT    020H,AL 
  95.     POP    DS 
  96.     POP    DX 
  97.     POP    BX 
  98.     POP    AX        ; Restore registers 
  99.     IRET            ; and return from interrupt 
  100. ; This table maps button values into ones appropriate for returning 
  101. ; to application 
  102. BUTMAP    DB    07,03,05,01,06,02,04,00 
  103. ; This routine returns button status = ax 
  104. ; a 1 bit indicates button presses 
  105. BUTTONSTAT: 
  106.     PUSH    BX 
  107.     MOV    AL,BSTAT 
  108.     AND    AX,7 
  109.     MOV    BX,OFFSET BUTMAP  ; convert to Microsoft format 
  110.     XLAT 
  111.     POP    BX 
  112. RETADR: 
  113.     RET 
  114. ; Motion Count routine 
  115. ;     on entry: 
  116. ;  Ax=cursor x,Bx=cursor y (Ignored by this driver) 
  117. ;      on exit: 
  118. ;  Ax=delta  x,Bx=delta y 
  119. MOTIONCOUNT: 
  120.     CALL    QREADPACKET    ; Read a packet 
  121.     MOV    BX,NEWY        ; return y coordinates 
  122.     NEG    BX        ; positive coordinate move down the screen 
  123.     MOV    AX,NEWX        ; return x 
  124.     RET 
  125. ; clean up the serial port interrupts and masks 
  126. DSERIAL: 
  127.     CLI 
  128.     IN    AL,021H        ; Read interupt mask             
  129.     MOV    AH,IMSK        ; clear appropriate int 
  130.     NOT    AH        ; by setting bits 
  131.     OR    AL,AH 
  132.     OUT    021H,AL        ; write it out 
  133.     MOV    DX,CPORT    ; get port address 
  134.     ADD    DX,3        ; line control register 
  135.     IN    AL,DX        ; fetch it 
  136.     AND    AL,07FH        ; set low to access interrupt  
  137.     OUT    DX,AL        ; enable register 
  138.     SUB    DX,2        ; point at interrupt enable register 
  139.     SUB    AL,AL        ; clear 
  140.     OUT    DX,AL        ; it 
  141.     ADD    DX,3        ; and clear 
  142.     OUT    DX,AL        ; modem control register 
  143.     STI            ; finished 
  144.     RET 
  145. GSERIAL: 
  146.     MOV    AX,COMNUM    ; returns com# 
  147.     INC    AX 
  148.     RET 
  149. ; This code intializes the mouse systems serial mouse 
  150. ; it takes the com number in Ax (1 = com1 2 = com2) 
  151. ISERIAL PROC    NEAR 
  152.     PUSH    CX 
  153.     DEC    AX 
  154.     MOV    COMNUM,AX        ; Save com# 
  155.     MOV    DX,AX 
  156.     MOV    AX,087H            ; 12K BAUD 
  157.     INT    SERIAL            ; let bios initialize baud rate and stuff 
  158.     PUSH    BX 
  159.     PUSH    DX 
  160.     PUSH    CS 
  161.     MOV    AX,040H            ; point at bios data segment 
  162.     MOV    DS,AX 
  163.     MOV    BX,DX 
  164.     SHL    BX,1 
  165.     MOV    DX,ZERO[BX]        ; Get port address at 40:0 or 40:2 
  166.     POP    DS 
  167.     MOV    CPORT,DX        ; save it 
  168.     CLI 
  169.     MOV    DX,OFFSET ISR        ; stick the serial interrupt handler 
  170.     MOV    AL,00CH            ; in either Int 0Ch, or 0Bh
  171.     MOV    AH,BYTE PTR COMNUM
  172.     AND    AH,1             ; only 2 Interrupts available
  173.     SUB    AL,AH
  174.     MOV    AH,025H            ; set interrupt request
  175.     INT    MSDOS 
  176.     IN    AL,021H            ; mask the interrup controller 
  177.     MOV    AH,0EFH 
  178.     CMP    COMNUM,0 
  179.     JZ    ISERIAL2 
  180.     MOV    AH,0F7H 
  181. ISERIAL2: 
  182.     MOV    IMSK,AH            ; Save for later 
  183.     AND    AL,AH 
  184.     OUT    021H,AL 
  185.     MOV    DX,CPORT        ; get port address 
  186.     ADD    DX,3            ; Line control register 
  187.     IN    AL,DX 
  188.     AND    AL,07FH            ; make interrupt enable register 
  189.     OUT    DX,AL            ; Addressable 
  190.     JMP    $+2            
  191.     SUB    DX,2            ; Point at it 
  192.     MOV    AL,1            ; set the data available int 
  193.     OUT    DX,AL 
  194.     ADD    DX,3             ; Modem control register 
  195.     MOV    AL,08            ; set it 
  196.     OUT    DX,AL 
  197.     STI 
  198.     MOV    DX,CPORT
  199.     IN    AL,DX            ; Clear receive buffer
  200.     POP    DX 
  201.     POP    BX 
  202.     POP    CX 
  203.     MOV    AL,-1            ; mouse available. 
  204.     RET 
  205. ISERIAL    ENDP 
  206. ;     
  207. QREADPACKET: 
  208. QR1: 
  209.     CLI 
  210.     NOP 
  211.     MOV    AX,XACCUM    ; get x accumulator 
  212.     MOV    NEWX,AX        ; move to new delta x 
  213.     MOV    AX,YACCUM    ; get y      
  214.     MOV    NEWY,AX        ; to new y 
  215.     SUB    BX,BX 
  216.     MOV    XACCUM,BX    ; clear  
  217.     MOV    YACCUM,BX    ; accumulators 
  218.     STI 
  219. QREXIT: 
  220.     RET 
  221.